﻿@using System.Web.Script.Serialization
@model Nop.Admin.Models.Shops.ShopModel 

@{
    
    var shopModel= Model;
    var shopModelObj = new HtmlString(new JavaScriptSerializer().Serialize(shopModel));
}



<form action="@Url.Action("Create", "Shop")" method="POST">

        <div>
            <label>ShopName</label>
            <input type="text" data-bind="value:ShopName"/>
        </div>
        <div>
            <label>Shop Moderator</label>
          <select data-bind="options: $root.ShopModerators, value: SelectedShopModerator, optionsText:'UserName'"></select>
        </div>


    
    <input type="button" data-bind="click:SaveShop" value="@T("Admin.Shop.Save")" />
    

</form>


<script>
    var initData = @shopModelObj;
    

    function validateShopForm() {
        //$.validity.start();

        //$('#UserName').require("pleaes input username");

        //var result = $.validity.end();
        //return result.valid;
        return true;
    }

    function ShopModerator(userId, userName) {
        this.Id = ko.observable(userId);
        this.UserName = ko.observable(userName);
    }
    
    function ShopViewModel() {
        var self = this;
        self.ShopName = initData.ShopName;

        self.ShopModerators = $.map(initData.NotUsedShopModerators, function(a) {
            return new ShopModerator(a.Id, a.Username);
        });



        self.SelectedShopModerator = ko.observable(initData.SelectedShopModerator.Id);
        
        self.SaveShop= function() {
            if (!validateShopForm()) {
                return;
            } else {
                $.ajax("@Url.Action("Create", "Shop")", {
                    data: ko.toJSON(self),
                    type: "post",
                    contentType: "application/json",
                    success: function(result) {
                        if (result.Status == "Success") {
                            alert('save success');
                            
                        }
                    }
                });
            }
         };

    }

    var obj = new ShopViewModel();
        
    ko.applyBindings(obj);           
</script>
